package heven.holt.library.extensions

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.content.res.Configuration

fun Context.getActivity(): Activity? {
    var context = this
    while (context is ContextWrapper) {
        if (context is Activity) return context
        context = context.baseContext
    }
    return null
}

fun Context.injectVConsoleJs(): String? {
    return try {
        val vconsoleJs = resources.assets.open("js/vconsole.min.js").use {
            val buffer = ByteArray(it.available())
            it.read(buffer)
            String(buffer)
        }
        """ $vconsoleJs
                 var vConsole = new VConsole();
            """.trimIndent()
    } catch (e: Exception) {
        e.printStackTrace()
        null
    }
}

fun Context.isNightMode(): Boolean {
    val config = resources.configuration
    val uiMode = config.uiMode and Configuration.UI_MODE_NIGHT_MASK
    return uiMode == Configuration.UI_MODE_NIGHT_YES
}

fun Context.getScreenWidth(): Int {
    val dm = resources.displayMetrics
    return dm.widthPixels
}

fun Context.getScreenHeight(): Int {
    val dm = resources.displayMetrics
    return dm.heightPixels
}
